''' <summary>
''' This is the main type for your game
''' </summary>
Public Class Game1
    Inherits Microsoft.Xna.Framework.Game

    Private WithEvents graphics As GraphicsDeviceManager
    Private WithEvents spriteBatch As SpriteBatch

    Dim GameMap As New Map
    Private Camera As New Camera2D

    Dim bg As Texture2D
    Public lucasTxtr As Texture2D
    Dim SpriteText As SpriteFont

    Dim Player1 As New Player 'PlatformerPlayer
    Dim ComputerTest(3) As Player
    Dim AI(3) As PrimativeAI

    Dim Light As New Color(50, 50, 50, 255)
    Dim ColBol As Boolean = True
    Dim R As Single = 210, g As Single = 210, b As Single = 210
    Dim GlobalLighting As Color


    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)
        Content.RootDirectory = "Content"
    End Sub

    ''' <summary>
    ''' Allows the game to perform any initialization it needs to before starting to run.
    ''' This is where it can query for any required services and load any non-graphic
    ''' related content.  Calling MyBase.Initialize will enumerate through any components
    ''' and initialize them as well.
    ''' </summary>
    Protected Overrides Sub Initialize()
        ' TODO: Add your initialization logic here
        graphics.PreferredBackBufferHeight = 720
        graphics.PreferredBackBufferWidth = 1280
        graphics.ApplyChanges()


        '#If DEBUG Then
        '        IsFixedTimeStep = False
        '        graphics.SynchronizeWithVerticalRetrace = False
        '        graphics.ApplyChanges()
        '#End If

        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' LoadContent will be called once per game and is the place to load
    ''' all of your content.
    ''' </summary>
    Protected Overrides Sub LoadContent()
        ' Create a new SpriteBatch, which can be used to draw textures.

        spriteBatch = New SpriteBatch(GraphicsDevice)
        bg = Me.Content.Load(Of Texture2D)("Textures/background/background")
        lucasTxtr = Me.Content.Load(Of Texture2D)("Textures/character/SpritesSheet") 'lucas") '_Pixified
        SpriteText = Me.Content.Load(Of SpriteFont)("sp")

        Dim Wall As Texture2D = Me.Content.Load(Of Texture2D)("Textures/blocks/metal")
        Dim Ground As Texture2D = Me.Content.Load(Of Texture2D)("Textures/blocks/coolestTile2") 'coblestone")
        Dim Decal As Texture2D = Me.Content.Load(Of Texture2D)("Textures/decals/TestDecal")

        Player1.Intialise(Me, GameMap, lucasTxtr)


        GameMap.IntialiseMap(Ground, Wall, Decal)

        'Dim rand As New Random
        'For i = 0 To ComputerTest.Length - 1
        '    ComputerTest(i) = New Player
        '    AI(i) = New PrimativeAI
        '    ComputerTest(i).Intialise(Me, GameMap, lucasTxtr)

        '    AI(i).IntialiseMovementArray("Still", rand) 'rand.Next(0, 3))
        'Next

        ' TODO: use Me.Content to load your game content here
    End Sub

    ''' <summary>
    ''' UnloadContent will be called once per game and is the place to unload
    ''' all content.
    ''' </summary>
    Protected Overrides Sub UnloadContent()
        ' TODO: Unload any non ContentManager content here
    End Sub

    ''' <summary>
    ''' Allows the game to run logic such as updating the world,
    ''' checking for collisions, gathering input, and playing audio.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Update(ByVal gameTime As GameTime)
        ' Allows the game to exit
        If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Escape) = True Then
            Me.Exit()
        End If

        'COMPUTER TEST AI
        '//STACK COMMANDS


        'For i = 0 To ComputerTest.Length - 1
        '    ComputerTest(i).AI_MovementHandler(AI(i).MovementArray(AI(i).Index))
        '    AI(i).IndexUpdate()
        'Next

        Player1.PlayerMovementHandler()



        If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Z) = True Then
            CameraBol = Not CameraBol
        End If

        If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.X) = True Then
            CamInt += 1
        End If

        If CamInt = 4 Then
            CamInt = 0
        End If

        If CameraBol = True Then
            Camera.Pos = Player1.Position
            'Camera.Rotation = -Player1.PlayerBearing
            MoveCamera(-Player1.Vector)
        Else
            Camera.Pos = ComputerTest(CamInt).Position
            MoveCamera(-ComputerTest(CamInt).Vector)
            'Camera.Rotation = -ComputerTest(CamInt).PlayerBearing
        End If



        MyBase.Update(gameTime)
    End Sub
    Dim CameraBol As Boolean = True
    Dim CamInt As Integer = 0

    Private Sub MoveCamera(speed As Vector2)
        ' make keyboard move the camera
        'If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Up) = True Then
        '    Camera._pos += (New Vector2(0, -speed.Y))
        'End If

        'If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Down) = True Then
        '    Camera._pos += (New Vector2(speed.X, 0))
        'End If

        'If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) = True Then
        '    Camera._pos += (New Vector2(0, speed.Y))
        'End If

        'If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) = True Then
        '    Camera._pos += (New Vector2(-speed.X, 0))
        'End If

        If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Q) = True Then
            Camera.Zoom += 0.05
        End If

        If Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.E) = True Then
            Camera.Zoom -= 0.05
        End If
    End Sub


    ''' <summary>
    ''' This is called when the game should draw itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Draw(ByVal gameTime As GameTime)
        GraphicsDevice.Clear(New Color(CInt((R) - 104), CInt((g) - 20), CInt((b) - 11))) 'New Color(R, g, b)) 'New Color(151 , 235 , 244 , 255)) 'New Color(155, 216, 255, 255))

        'If Light.R > 255 Then
        '    Light.R = 0
        'End If
        'If Light.G > 255 Then
        '    Light.G = 0
        'End If
        'If Light.B > 255 Then
        '    Light.B = 0
        'End If


        If R >= 254 Or R <= 40 Then 'Light.R >= 255 Or Light.G >= 255 Or Light.B >= 255 Then
            ColBol = Not ColBol
            'R = 100
            'g = 100
            'b = 100
            'Light.R = 10
            'Light.G = 10
            'Light.B = 10
        End If

        If ColBol = True Then
            If R > 245 Then
                R += 0.01
                g += 0.01
                b += 0.01
            Else
                R += 0.1
                g += 0.1
                b += 0.1
            End If
        ElseIf ColBol = False Then
            If R > 245 Then
                R -= 0.01
                g -= 0.01
                b -= 0.01
            Else
                R -= 0.1
                g -= 0.1
                b -= 0.1
            End If
        Else

        End If

        GlobalLighting = New Color(CInt(R), CInt(g), CInt(b), 255)

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, Nothing, Nothing, Nothing, Nothing, Camera.get_transformation(GraphicsDevice))
        GameMap.DrawMap(spriteBatch, GlobalLighting)
        spriteBatch.End()


        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, Nothing, Nothing, Nothing, Nothing, Camera.get_transformation(GraphicsDevice))
        Player1.DrawPlayer(GraphicsDevice, spriteBatch, lucasTxtr, GlobalLighting)
        spriteBatch.End()


        'For i = 0 To 3
        '    spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, Nothing, Nothing, Nothing, Nothing, Camera.get_transformation(GraphicsDevice))
        '    ComputerTest(i).DrawPlayer(spriteBatch, lucasTxtr, GlobalLighting)
        '    spriteBatch.End()

        '    ComputerTest(i).DebugText(spriteBatch, SpriteText, i + 1)
        'Next



        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, Nothing, Nothing, Nothing, Nothing, Camera.get_transformation(GraphicsDevice))
        spriteBatch.End()

        Player1.DebugText(spriteBatch, SpriteText, 0)


        DrawDebugText(gameTime)

        MyBase.Draw(gameTime)
    End Sub


    Sub DrawDebugText(gametime)
        Dim fps = Math.Round(1 / gametime.ElapsedGameTime.TotalSeconds)
        spriteBatch.Begin()
        spriteBatch.DrawString(SpriteText, "R: " & CInt(R) & " G: " & CInt(g) & " B: " & CInt(b), New Vector2(0, 75), Color.White)
        spriteBatch.DrawString(SpriteText, "FPS: " & fps, New Vector2(300, 75), Color.White)
        spriteBatch.End()
    End Sub
End Class
